package com.xbao.service.impl;

import com.github.pagehelper.PageHelper;
import com.xbao.core.http.CommRequest;
import com.xbao.core.util.MyBeanUtil;
import com.xbao.core.util.pub.tool.StringUtils;
import com.xbao.dao.SubjectMapper;
import com.xbao.model.Subject;
import com.xbao.model.SubjectExample;
import com.xbao.model.req.SubjectReq;
import com.xbao.model.vo.SubjectVo;
import com.xbao.service.SubjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by qingshengzheng on 18/8/26.
 */
@Service
//@Cacheable(value = "subject")
public class SubjectServiceImpl implements SubjectService {
    @Resource
    private SubjectMapper subjectMapper;

    /**
     * @param subject
     */
    @Override
    public void addSubject(Subject subject) {
        subjectMapper.insert(subject);

    }

    /**
     * @param sid µ
     */
    @Override
    public void deleteSubject(Long sid) {
        subjectMapper.deleteByPrimaryKey(sid);
    }

    /**
     * @param subject
     */
    //@CachePut(key="#subject.sid")
    @Override
    public void modifySubject(Subject subject) {
        subjectMapper.updateByPrimaryKey(subject);
    }

    /**
     * @return
     */
    @Override
    // @Cacheable(key = "#id")
    public SubjectVo detailSubject(Long id) {
        Subject subject = subjectMapper.selectByPrimaryKey(id);
        SubjectVo vo = MyBeanUtil.copyProperties(subject, SubjectVo.class);
        return vo;
    }

    /**
     * @param subjectReqCommRequest
     * @return
     */
    @Override
    public List<Subject> querySubjects(SubjectReq subjectReqCommRequest) {
        SubjectExample example = new SubjectExample();
        SubjectExample.Criteria criteria = example.createCriteria();
        if (subjectReqCommRequest != null) {
            if (subjectReqCommRequest.getPager() != null) {
                PageHelper.startPage(subjectReqCommRequest.getPager().getPageNo(), subjectReqCommRequest.getPager().getPageSize());
                example.setOrderByClause("id desc");
            }
            if (!StringUtils.isNull(subjectReqCommRequest.getName()))
                criteria.andNameLike("%" + subjectReqCommRequest.getName() + "%");
        }

        List<Subject> subjects = subjectMapper.selectByExample(example);
        return subjects;
    }


    @Override
    //@Cacheable(key="'all_subject'+#orgid")
    public List<Subject> allSubjects(Long orgid) {
        SubjectExample example = new SubjectExample();
        SubjectExample.Criteria criteria = example.createCriteria();
        List<Subject> subs = subjectMapper.selectByExample(example);
        return subs;
    }


    @Override
    public List<Subject> querySubjectsPage(CommRequest<SubjectReq> subjectReqCommRequest) {
        SubjectExample example = new SubjectExample();
        SubjectExample.Criteria criteria = example.createCriteria();
        if (subjectReqCommRequest.getLimit() == null)
            subjectReqCommRequest.setLimit(10);
        example.setLimit(subjectReqCommRequest.getLimit());
        List<Subject> subjects = subjectMapper.selectByExample(example);
        return subjects;
    }
}
